﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using MangaManagerLib;
using System.Text.RegularExpressions;
namespace KissMangaReader
{
    
    [MangaPluginAttribute("get data from kissmanga website")]
    public class KissMangaReader: IMangaOperation
    {
        #region IMangaOperation Members
        
        public IList<string> GetDataForChapter(string url)
        {
            IList<string> resultValue = new List<string>();
           
            string sourceCode = WebWorker.GetContent(url);
            int startIndex = sourceCode.IndexOf("lstImages.push(\"");
            if(startIndex < 1)
            {
                return null;
            }
            sourceCode = sourceCode.Substring(startIndex, sourceCode.Length - startIndex);
            while (sourceCode.IndexOf("lstImages.push(\"") != -1)
            {
                startIndex = sourceCode.IndexOf("lstImages.push(\"") + 16;
                int endIndex = sourceCode.IndexOf("\");", startIndex);
                string link = sourceCode.Substring(startIndex, endIndex - startIndex);
                resultValue.Add(link);
                sourceCode = sourceCode.Replace(string.Format("lstImages.push(\"{0}", link), "");
            }
            return resultValue;
        }

        public MangaStoryDetail GetMangaStoryDetail(string url)
        {
            MangaStoryDetail mangaStoryDetail = new MangaStoryDetail();
            mangaStoryDetail.Chapters = new List<Chapter>();
            if (!url.Contains("confirm=yes"))
            {
                url = String.Format("{0}?confirm=yes", url);
            }
            string urlHost = WebWorker.GetURLHost(url);
            string sourceCode = WebWorker.GetContent(url).Replace("\r", "").Replace("\n", "").Replace("\t", "");
            string linkRegex = @"<a\s*[\w]*=""bigChar""\s*href=""([\(\)""&/\s\’\%\$\?\[\]\{\}\,\!\@\#\*\d\w:;'=+\-–\.\\]*)""[^>]*>([\(\)""&/\s\’\%\$\?\[\]\{\}\,\!\@\#\*\d\w:;'=+\-–\.\\]*)</a>";
            Match linkMatch = Regex.Match(sourceCode, linkRegex, RegexOptions.IgnoreCase);
            sourceCode.Replace("&nbsp;", string.Empty);
            if (linkMatch.Success)
            {
                mangaStoryDetail.Link = string.Format("{0}{1}", urlHost, linkMatch.Groups[1].Value);
                mangaStoryDetail.Title = linkMatch.Groups[2].Value;
            }
            //get other name
            if(sourceCode.Contains(@"Other name:"))
            {
                string otherNamePara = sourceCode.Substring(sourceCode.IndexOf("Other name:"), sourceCode.Length - sourceCode.IndexOf("Other name:"));
                otherNamePara = otherNamePara.Substring(0, otherNamePara.IndexOf(@"</p"));
                string otherNameRegex = @"<a\s*href=""([/\.\s\d\w&%’?'""%=+\-–]*)""\s*title=""[\(\)""&/\s\%\$\?\[\]’\{\}\,\!\@\#\*\d\w:;'=+\-–\.\\]*""[^>]*\s*>([\(\)""&/\s\%\$\?\[\]\{\}\,\!\@\#\*\d’\w:;'–=+\-–\.\\]*)</a>";
                MatchCollection otherNameMatches = Regex.Matches(otherNamePara, otherNameRegex, RegexOptions.IgnoreCase);
                foreach (Match match in otherNameMatches)
                {
                    mangaStoryDetail.OtherName = string.Format("{0}; {1}", match.Groups[2], mangaStoryDetail.OtherName );
                }
            }
            //get Genres
            if (sourceCode.Contains("Genres:"))
            {
                string genresPara = sourceCode.Substring(sourceCode.IndexOf("Genres:"), sourceCode.Length - sourceCode.IndexOf("Genres:"));
                genresPara = genresPara.Substring(0, genresPara.IndexOf(@"</p"));
                string genresRegex = @"<a\s*href=""([/\.\s\d\w&%’?'""%=+\-–]*)""\s*title=""[\(\)""&/\s\%\$\?\[\]’\{\}\,\!\@\#\*\d\w:;'=+\-–\.\\]*""[^>]*\s*>([\(\)""&/\s\%\$\?\[\]\{\}\,\!\@\#\*\d’\w:;'–=+\-–\.\\]*)</a>";
                MatchCollection genresMatches = Regex.Matches(genresPara, genresRegex, RegexOptions.IgnoreCase);
                foreach (Match match in genresMatches)
                {
                    mangaStoryDetail.Genres = string.Format("{0}; {1}", match.Groups[2], mangaStoryDetail.Genres);
                }
            }
            //get status
            string statusRegex = @"Status:</span>(?<Status>[&;\d\w]*[^<]*)<";
            Match statusMatch = Regex.Match(sourceCode, statusRegex, RegexOptions.IgnoreCase);
            if(statusMatch.Success)
            {
                mangaStoryDetail.Status = statusMatch.Groups["Status"].Value.TrimStart().TrimEnd();
            }
            //get views
            string viewsRegex = @"Views:</span>(?<Views>[&;\d\w]*[^<]*)<";
            Match viewsMatch = Regex.Match(sourceCode, viewsRegex, RegexOptions.IgnoreCase);
            if (viewsMatch.Success)
            {
                mangaStoryDetail.Views = viewsMatch.Groups["Views"].Value.TrimStart().TrimEnd();
            }

            //get author
            if (sourceCode.Contains("Author:"))
            {
                string authorPara = sourceCode.Substring(sourceCode.IndexOf("Author:"), sourceCode.Length - sourceCode.IndexOf("Author:"));
                authorPara = authorPara.Substring(0, authorPara.IndexOf(@"</p"));
                string authorRegex = @"<a\s*href=""([/\.\s\d\w&%’?'""%=+\-–]*)""\s*title=""[\(\)""&/\s\%\$\?\[\]’\{\}\,\!\@\#\*\d\w:;'=+\-–\.\\]*""[^>]*\s*>([\(\)""&/\s\%\$\?\[\]\{\}\,\!\@\#\*\d’\w:;'–=+\-–\.\\]*)</a>";
                MatchCollection authorMatches = Regex.Matches(authorPara, authorRegex, RegexOptions.IgnoreCase);
                foreach (Match match in authorMatches)
                {
                    mangaStoryDetail.Author = string.Format("{0}; {1}", match.Groups[2], mangaStoryDetail.Author);
                }
            }            
            //get summary
            //linkRegex = @"Summary:</span>[\s*(</p>)*]*[^(<p)]<p[\s\w\d""'’~=+%/&?\-–;]*[^>]*>([\(\)""&/\s\^\’<\>\%\$\?\[\]\{\}\,\!\@\#\*\d\w:;'=+\-–~\.\\]*)[^(</p>)]</p>";
            linkRegex = @"Summary:</span>[\s*(</p>)*]*[^(<p)]<p[\s\w\d""'’~=+%/&?\-–;]*[^>]*>(.*?)</p>";
            linkMatch = Regex.Match(sourceCode, linkRegex, RegexOptions.IgnoreCase);
            if (linkMatch.Success)
            {
                mangaStoryDetail.Summary = linkMatch.Groups[1].Value;
                if (mangaStoryDetail.Summary.Contains("</p>"))
                {
                    int indexNeedRemove = mangaStoryDetail.Summary.IndexOf("</p>");
                    mangaStoryDetail.Summary = WebWorker.RemoveHTMLTag(mangaStoryDetail.Summary.Substring(0, indexNeedRemove));
                }
                else
                {
                    mangaStoryDetail.Summary = WebWorker.RemoveHTMLTag(mangaStoryDetail.Summary);
                }
            }
            linkRegex = @"Image</div>\s*<div[\s\w\d""’':=+%/&?\-–]*[^>]*>\s*<div[\s\w\d"":’'=+%/&?\-–]*[^>]*>[\s\w\d;’:&]*</div>\s*<div[\s\w\d""':’=+%/&?\-–]*[^>]*>\s*"
                        + @"<img\s*(width=""[\d\w]*"")?\s?(height=""[\d\w]*"")?\s?src=""(?<Image>[/\.\s\d\w&?%_:–;=+\-–]*)""(\s*)[^(/>)]/>";
            linkMatch = Regex.Match(sourceCode, linkRegex, RegexOptions.IgnoreCase);
            if (linkMatch.Success)
            {
                mangaStoryDetail.ImageLink = linkMatch.Groups["Image"].Value;
            }
            //get chapters
            string chaptersRssRegex = @"href=""(?<RssLink>http://kissmanga.com/RSS/Manga/[\d\w&%’?'""%=+\-–]*)""";
            Match chaptersRssMatch = Regex.Match(sourceCode, chaptersRssRegex, RegexOptions.IgnoreCase);
            if(chaptersRssMatch.Success)
            {
                string rssChapters = WebWorker.GetContent(chaptersRssMatch.Groups["RssLink"].Value);
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(rssChapters);
                XmlNodeList itemNodeList = xmlDoc.DocumentElement.SelectNodes(@"/rss/channel/item");
                foreach (XmlNode nodeItem in itemNodeList)
                {
                    Chapter c= new Chapter();
                    c.Date = nodeItem.SelectSingleNode(@"pubDate").InnerText;
                    c.Link = nodeItem.SelectSingleNode(@"link").InnerText;
                    c.Title = nodeItem.SelectSingleNode(@"title").InnerText;
                    mangaStoryDetail.Chapters.Add(c);
                }
            }
            //string chaptersRegex = @"<td>\s*<a\s*href=""([/\.\s\d\w&%’?'""%=+\-–]*)""\s*title=""[\(\)""&/\s\%\$\?\[\]’\{\}\,\!\@\#\*\d\w:;'=+\-–\.\\]*""[^>]*\s*>([\(\)""&/\s\%\$\?\[\]\{\}\,\!\@\#\*\d’\w:;'–=+\-–\.\\]*)</a>\s*</td>\s*<td>\s*(\d*/\d*/\d*)\s*</td>";
            //MatchCollection chaptersMatch = Regex.Matches(sourceCode, chaptersRegex, RegexOptions.IgnoreCase);
            //foreach (Match m in chaptersMatch)
            //{
            //    Chapter c = new Chapter();
            //    c.Date = m.Groups[3].Value;
            //    c.Link = string.Format("{0}{1}", urlHost, m.Groups[1].Value);
            //    c.Title = m.Groups[2].Value.TrimStart().TrimEnd();
            //    mangaStoryDetail.Chapters.Add(c);
            //}
            return mangaStoryDetail;
        }

        public MangaCollection GetMangaCollection(string url, int page)
        {
            MangaCollection mangaCollection = new MangaCollection();
            mangaCollection.CurrentPage = page;
            mangaCollection.MangaList = new List<MangaStory>();
            string sourceCode = WebWorker.GetContent(string.Format("{0}?page={1}", url, page) );
            string lastPageRegex = @"page=[""]*([\d]*)[""]*[^>]*>[&\d\w\s;:]*(»)*[&\d\w\s;:]*Last\s*</a>";
            Regex lastPageRgx = new Regex(lastPageRegex, RegexOptions.IgnoreCase);
            Match lastPageMatch = lastPageRgx.Match(sourceCode, 0);
            if (lastPageMatch.Success)
            {
                try
                {
                    mangaCollection.TotalPage = int.Parse(lastPageMatch.Groups[1].Value);
                }
                catch (Exception ex)
                {
                    Logger.GetInstance().LogException(ex);
                }
            }
            //listMangaRegex  = @"\<table[a-zA-Z0-9='""\>\<\s\\\[\^\$\.\|\?\*\+\(\)\{\}]*";
            sourceCode = sourceCode.Substring(sourceCode.IndexOf(@"<table class=""listing"">"), sourceCode.Length - sourceCode.IndexOf(@"<table class=""listing"">"));
            sourceCode = sourceCode.Substring(0, sourceCode.IndexOf(@"</table>"));
            string mangaRegex = @"title='\s*<img\s*(width=""\d*"")?\s*(height=""\d*"")?\s*src=""(?<ImageLink>[\/\.\s\d\w&?%:'=_+\–\-]*)""\s*style=""[\/\.\s\d\w&?%:;'""=_+\–\-]*""\s*/>\s*"
                                + @"<div[\s\w\d:""'+_;\-\–]*[^>]*>\s*"
                                + @"<a\s*class=""bigChar""\s*href=""(?<MangaLink>[\/\.\s\d\w&?%:'""=_+\–\-]*)"">(?<MangaTitle>[\(\)\/\.\s\d\`~\{\}|\@\w&!#$\^?%,:'""=_+\–\-]*)</a>\s*"
                                //+ @"<p>([\(\)\/\.\s\d\`~\{\}\|\@\w&!#$\^?%,:'""=_+\–\-.]*)[^(</p>)]*</p>\s*"
                                //+ @"</div>'>\s*(?<MangaTitle>[\(\)\/\.\s\d\`~\{\}|\@\w&!#$\^?%,:'""=_+\–\-.]*)[^(</a>)]*</a>\s*"
                                //+ @"([\(\)\/\.\s\d\`~\<\>\{\}\|\@\w&!#$\^?%,:'""=_+\–\-.]*)[^(</td>\s*<td>)]*</td>\s*<td>\s*"
                                //+ @"(<a\s*href=""(?<ChapterLink>[\/\.\s\d\w\[\]&?%:'""=_+\–\-]*)"">(?<ChapterTitle>[\(\)\/\.\s\d\`~\{\}\[\]\|\@\w&!#$\^?%,:'""=_+\–\-]*)</a>)?\s?(Completed)?\s?"
                                ;
            sourceCode = sourceCode.Replace("\r", "").Replace("\n", "").Replace("\t", "");
            Regex mangaRgx = new Regex(mangaRegex, RegexOptions.IgnoreCase);
            MatchCollection mangaMatches = mangaRgx.Matches(sourceCode, 0);
            Console.WriteLine(mangaMatches.Count);
            //produce manga object
            foreach (Match mt in mangaMatches)
            {
                MangaStory mangaStory = new MangaStory();
                mangaStory.Link = string.Format("{0}{1}", WebWorker.GetURLHost(url), mt.Groups["MangaLink"].Value);
                mangaStory.Title = mt.Groups["MangaTitle"].Value.TrimStart().TrimEnd();
                mangaStory.ImageLink = mt.Groups["ImageLink"].Value;

                string trPara = sourceCode.Substring(sourceCode.IndexOf(mt.Value),
                                                     sourceCode.Length - sourceCode.IndexOf(mt.Value));
                trPara = trPara.Substring(trPara.IndexOf(@"</td>"), trPara.IndexOf(@"</tr>") - trPara.IndexOf(@"</td>"));

                if (!trPara.Contains("<a href"))
                {
                    mangaStory.Status = "Completed";
                }
                else
                {
                    mangaStory.ChapterNewest = new Chapter();
                    mangaStory.Status = "Ongoging";
                    string chapterRegex =
                        @"<a\s*href=""(?<ChapterLink>[\/\.\s\d\w\[\]&?%:'""=_+\–\-]*)"">(?<ChapterTitle>[\(\)\/\.\s\d\`~\{\}\[\]\|\@\w&!#$\^?%,:'""=_+\–\-]*)</a>";
                    Match chapterMatch = Regex.Match(trPara, chapterRegex, RegexOptions.IgnoreCase);
                    if (chapterMatch.Success)
                    {
                        mangaStory.ChapterNewest.Link = string.Format("{0}{1}", WebWorker.GetURLHost(url),
                                                                      chapterMatch.Groups["ChapterLink"].Value);
                        mangaStory.ChapterNewest.Title = chapterMatch.Groups["ChapterTitle"].Value.TrimStart().TrimEnd();
                    }
                }
                mangaCollection.MangaList.Add(mangaStory);
            }

            
            return mangaCollection;
        }
        
        public IList<MangaStory> GetNewestManga(int numberToGet)
        {
            IList<MangaStory> result = new List<MangaStory>();
            string newestLink = @"http://kissmanga.com/MangaList/Newest?page={0}";
            int pageNumber = 1;
            while(result.Count < numberToGet)
            {
                string urlRequest = string.Format(newestLink, pageNumber);
                string sourceCode = WebWorker.GetContent(urlRequest).Replace("\r", "").Replace("\n", "").Replace("\t", "");
                if(sourceCode.IndexOf(@"<table class=""listing"">", System.StringComparison.Ordinal) < 1)
                {
                    return result;
                }
                sourceCode = sourceCode.Substring(sourceCode.IndexOf(@"<table class=""listing"">"), sourceCode.Length - sourceCode.IndexOf(@"<table class=""listing"">"));
                sourceCode = sourceCode.Substring(0, sourceCode.IndexOf(@"</table>"));
                string mangaRegex = @"title='\s*<img\s*(width=""\d*"")?\s*(height=""\d*"")?\s*src=""(?<ImageLink>[\/\.\s\d\w&?%:'=_+\–\-]*)""\s*style=""[\/\.\s\d\w&?%:;'""=_+\–\-]*""\s*/>\s*"
                                    + @"<div[\s\w\d:""'+_;\-\–]*[^>]*>\s*"
                                    + @"<a\s*class=""bigChar""\s*href=""(?<MangaLink>[\/\.\s\d\w&?%:'""=_+\–\-]*)"">(?<MangaTitle>[\(\)\/\.\s\d\`~\{\}|\@\w&!#$\^?%,:'""=_+\–\-]*)</a>\s*"
                                    //+ @"<p>([\(\)\/\.\s\d\`~\{\}\|\@\w&!#$\^?%,:'""=_+\–\-.]*)[^(</p>)]*</p>\s*"
                                    //+ @"</div>'>\s*(?<MangaTitle>[\(\)\/\.\s\d\`~\{\}|\@\w&!#$\^?%,:'""=_+\–\-.]*)[^(</a>)]*</a>\s*"
                                    //+ @"([\(\)\/\.\s\d\`~\<\>\{\}\|\@\w&!#$\^?%,:'""=_+\–\-.]*)[^(</td>\s*<td>)]*</td>\s*<td>\s*"
                                    //+ @"(<a\s*href=""(?<ChapterLink>[\/\.\s\d\w\[\]&?%:'""=_+\–\-]*)"">(?<ChapterTitle>[\(\)\/\.\s\d\`~\{\}\[\]\|\@\w&!#$\^?%,:'""=_+\–\-]*)</a>)?\s?(Completed)?\s?"
                                    ;
                sourceCode = sourceCode.Replace("\r", "").Replace("\n", "").Replace("\t", "");
                Regex mangaRgx = new Regex(mangaRegex, RegexOptions.IgnoreCase);
                MatchCollection mangaMatches = mangaRgx.Matches(sourceCode, 0);
                Console.WriteLine(mangaMatches.Count);
                //produce manga object
                foreach (Match mt in mangaMatches)
                {
                    MangaStory mangaStory = new MangaStory();
                    mangaStory.Link = string.Format("{0}{1}", WebWorker.GetURLHost(urlRequest), mt.Groups["MangaLink"].Value);
                    mangaStory.Title = mt.Groups["MangaTitle"].Value.TrimStart().TrimEnd();
                    mangaStory.ImageLink = mt.Groups["ImageLink"].Value;

                    string trPara = sourceCode.Substring(sourceCode.IndexOf(mt.Value), sourceCode.Length - sourceCode.IndexOf(mt.Value));
                    trPara = trPara.Substring(trPara.IndexOf(@"</td>"), trPara.IndexOf(@"</tr>") - trPara.IndexOf(@"</td>"));
                    
                    if (!trPara.Contains("<a href"))
                    {
                        mangaStory.Status = "Completed";
                    }
                    else
                    {
                        mangaStory.ChapterNewest = new Chapter();
                        mangaStory.Status = "Ongoging";
                        string chapterRegex = @"<a\s*href=""(?<ChapterLink>[\/\.\s\d\w\[\]&?%:'""=_+\–\-]*)"">(?<ChapterTitle>[\(\)\/\.\s\d\`~\{\}\[\]\|\@\w&!#$\^?%,:'""=_+\–\-]*)</a>";
                        Match chapterMatch = Regex.Match(trPara, chapterRegex, RegexOptions.IgnoreCase);
                        if (chapterMatch.Success)
                        {
                            mangaStory.ChapterNewest.Link = string.Format("{0}{1}", WebWorker.GetURLHost(urlRequest), chapterMatch.Groups["ChapterLink"].Value);
                            mangaStory.ChapterNewest.Title = chapterMatch.Groups["ChapterTitle"].Value.TrimStart().TrimEnd();
                        }
                    }
                    result.Add(mangaStory);
                    if(result.Count == numberToGet)
                    {
                        return result;
                    }
                }
                pageNumber++;
            }
            return result;
        }
        public IList<MangaStory> GetPopularManga(int numberToGet)
        {
            IList<MangaStory> result = new List<MangaStory>();
            string popularLink = @"http://kissmanga.com/MangaList/MostPopular?page={0}";
            int pageNumber = 1;
            while (result.Count < numberToGet)
            {
                string urlRequest = string.Format(popularLink, pageNumber);
                string sourceCode = WebWorker.GetContent(urlRequest).Replace("\r", "").Replace("\n", "").Replace("\t", "");
                if (sourceCode.IndexOf(@"<table class=""listing"">", System.StringComparison.Ordinal) < 1)
                {
                    return result;
                }
                sourceCode = sourceCode.Substring(sourceCode.IndexOf(@"<table class=""listing"">"), sourceCode.Length - sourceCode.IndexOf(@"<table class=""listing"">"));
                sourceCode = sourceCode.Substring(0, sourceCode.IndexOf(@"</table>"));
                string mangaRegex = @"title='\s*<img\s*(width=""\d*"")?\s*(height=""\d*"")?\s*src=""(?<ImageLink>[\/\.\s\d\w&?%:'=_+\–\-]*)""\s*style=""[\/\.\s\d\w&?%:;'""=_+\–\-]*""\s*/>\s*"
                                    + @"<div[\s\w\d:""'+_;\-\–]*[^>]*>\s*"
                                    + @"<a\s*class=""bigChar""\s*href=""(?<MangaLink>[\/\.\s\d\w&?%:'""=_+\–\-]*)"">(?<MangaTitle>[\(\)\/\.\s\d\`~\{\}|\@\w&!#$\^?%,:'""=_+\–\-]*)</a>\s*"
                                    //+ @"<p>([\(\)\/\.\s\d\`~\{\}\|\@\w&!#$\^?%,:'""=_+\–\-.]*)[^(</p>)]*</p>\s*"
                                    //+ @"</div>'>\s*(?<MangaTitle>[\(\)\/\.\s\d\`~\{\}|\@\w&!#$\^?%,:'""=_+\–\-.]*)[^(</a>)]*</a>\s*"
                                    //+ @"([\(\)\/\.\s\d\`~\<\>\{\}\|\@\w&!#$\^?%,:'""=_+\–\-.]*)[^(</td>\s*<td>)]*</td>\s*<td>\s*"
                                    //+ @"(<a\s*href=""(?<ChapterLink>[\/\.\s\d\w\[\]&?%:'""=_+\–\-]*)"">(?<ChapterTitle>[\(\)\/\.\s\d\`~\{\}\[\]\|\@\w&!#$\^?%,:'""=_+\–\-]*)</a>)?\s?(Completed)?\s?"
                                    ;
                sourceCode = sourceCode.Replace("\r", "").Replace("\n", "").Replace("\t", "");
                Regex mangaRgx = new Regex(mangaRegex, RegexOptions.IgnoreCase);
                MatchCollection mangaMatches = mangaRgx.Matches(sourceCode, 0);
                Console.WriteLine(mangaMatches.Count);
                //produce manga object
                foreach (Match mt in mangaMatches)
                {
                    MangaStory mangaStory = new MangaStory();
                    mangaStory.Link = string.Format("{0}{1}", WebWorker.GetURLHost(urlRequest), mt.Groups["MangaLink"].Value);
                    mangaStory.Title = mt.Groups["MangaTitle"].Value.TrimStart().TrimEnd();
                    mangaStory.ImageLink = mt.Groups["ImageLink"].Value;

                    string trPara = sourceCode.Substring(sourceCode.IndexOf(mt.Value), sourceCode.Length - sourceCode.IndexOf(mt.Value));
                    trPara = trPara.Substring(trPara.IndexOf(@"</td>"), trPara.IndexOf(@"</tr>") - trPara.IndexOf(@"</td>"));

                    if (!trPara.Contains("<a href"))
                    {
                        mangaStory.Status = "Completed";
                    }
                    else
                    {
                        mangaStory.ChapterNewest = new Chapter();
                        mangaStory.Status = "Ongoging";
                        string chapterRegex = @"<a\s*href=""(?<ChapterLink>[\/\.\s\d\w\[\]&?%:'""=_+\–\-]*)"">(?<ChapterTitle>[\(\)\/\.\s\d\`~\{\}\[\]\|\@\w&!#$\^?%,:'""=_+\–\-]*)</a>";
                        Match chapterMatch = Regex.Match(trPara, chapterRegex, RegexOptions.IgnoreCase);
                        if (chapterMatch.Success)
                        {
                            mangaStory.ChapterNewest.Link = string.Format("{0}{1}", WebWorker.GetURLHost(urlRequest), chapterMatch.Groups["ChapterLink"].Value);
                            mangaStory.ChapterNewest.Title = chapterMatch.Groups["ChapterTitle"].Value.TrimStart().TrimEnd();
                        }
                    }
                    result.Add(mangaStory);
                    if (result.Count == numberToGet)
                    {
                        return result;
                    }
                }
                pageNumber++;
            }
            return result;
        }
        public IList<MangaStory> SearchMangaByTitle(string key)
        {
            IList<MangaStory> result = new List<MangaStory>();
            string searchLink = @"http://kissmanga.com/Search/Manga";
            Dictionary<String, String> dictionary = new Dictionary<string, string>();
            dictionary.Add("keyword", key);
            string sourceCode = WebWorker.ExecutePostRequest(searchLink, dictionary).Replace("\r", "").Replace("\n", "").Replace("\t", "");
            if (sourceCode.IndexOf(@"<table class=""listing"">", System.StringComparison.Ordinal) < 1)
            {
                return result;
            }
            sourceCode = sourceCode.Substring(sourceCode.IndexOf(@"<table class=""listing"">"), sourceCode.Length - sourceCode.IndexOf(@"<table class=""listing"">"));
            sourceCode = sourceCode.Substring(0, sourceCode.IndexOf(@"</table>"));
            string mangaRegex = @"title='\s*<img\s*(width=""\d*"")?\s*(height=""\d*"")?\s*src=""(?<ImageLink>[\/\.\s\d\w&?%:'=_+\–\-]*)""\s*style=""[\/\.\s\d\w&?%:;'""=_+\–\-]*""\s*/>\s*"
                                + @"<div[\s\w\d:""'+_;\-\–]*[^>]*>\s*"
                                + @"<a\s*class=""bigChar""\s*href=""(?<MangaLink>[\/\.\s\d\w&?%:'""=_+\–\-]*)"">(?<MangaTitle>[\(\)\/\.\s\d\`~\{\}|\@\w&!#$\^?%,:'""=_+\–\-]*)</a>\s*"
                                //+ @"<p>([\(\)\/\.\s\d\`~\{\}\|\@\w&!#$\^?%,:'""=_+\–\-.]*)[^(</p>)]*</p>\s*"
                                //+ @"</div>'>\s*(?<MangaTitle>[\(\)\/\.\s\d\`~\{\}|\@\w&!#$\^?%,:'""=_+\–\-.]*)[^(</a>)]*</a>\s*"
                                //+ @"([\(\)\/\.\s\d\`~\<\>\{\}\|\@\w&!#$\^?%,:'""=_+\–\-.]*)[^(</td>\s*<td>)]*</td>\s*<td>\s*"
                                //+ @"(<a\s*href=""(?<ChapterLink>[\/\.\s\d\w\[\]&?%:'""=_+\–\-]*)"">(?<ChapterTitle>[\(\)\/\.\s\d\`~\{\}\[\]\|\@\w&!#$\^?%,:'""=_+\–\-]*)</a>)?\s?(Completed)?\s?"
                                ;
            sourceCode = sourceCode.Replace("\r", "").Replace("\n", "").Replace("\t", "");
            Regex mangaRgx = new Regex(mangaRegex, RegexOptions.IgnoreCase);
            MatchCollection mangaMatches = mangaRgx.Matches(sourceCode, 0);
            //produce manga object
            foreach (Match mt in mangaMatches)
            {
                MangaStory mangaStory = new MangaStory();
                mangaStory.Link = string.Format("{0}{1}", WebWorker.GetURLHost(searchLink), mt.Groups["MangaLink"].Value);
                mangaStory.Title = mt.Groups["MangaTitle"].Value.TrimStart().TrimEnd();
                mangaStory.ImageLink = mt.Groups["ImageLink"].Value;

                string trPara = sourceCode.Substring(sourceCode.IndexOf(mt.Value), sourceCode.Length - sourceCode.IndexOf(mt.Value));
                trPara = trPara.Substring(trPara.IndexOf(@"</td>"), trPara.IndexOf(@"</tr>") - trPara.IndexOf(@"</td>"));
                if (!trPara.Contains("<a href"))
                {
                    mangaStory.Status = "Completed";
                }
                else
                {
                    mangaStory.ChapterNewest = new Chapter();
                    mangaStory.Status = "Ongoging";
                    string chapterRegex = @"<a\s*href=""(?<ChapterLink>[\/\.\s\d\w\[\]&?%:'""=_+\–\-]*)"">(?<ChapterTitle>[\(\)\/\.\s\d\`~\{\}\[\]\|\@\w&!#$\^?%,:'""=_+\–\-]*)</a>";
                    Match chapterMatch = Regex.Match(trPara, chapterRegex, RegexOptions.IgnoreCase);
                    if (chapterMatch.Success)
                    {
                        mangaStory.ChapterNewest.Link = string.Format("{0}{1}", WebWorker.GetURLHost(searchLink), chapterMatch.Groups["ChapterLink"].Value);
                        mangaStory.ChapterNewest.Title = chapterMatch.Groups["ChapterTitle"].Value.TrimStart().TrimEnd();
                    }
                }
                result.Add(mangaStory);
                
            }
          
            return result;
        }
        #endregion
    }
}
